package cerebrum.common;

import cerebrum.controller.wallpaper.FirstRunImagesWrite;
import cerebrum.enums.SetUpMeaages;

import java.io.File;
import java.time.LocalDate;

public interface FilePathUtil {

    //获取昨日图片路径
    public static String getYesterDayImagesPath() {
        LocalDate newDate = LocalDate.now();
        LocalDate yesterDayDate = newDate.minusDays(1);
        //得到上面图片下载保存的地址
        return SetUpMeaages.IMAGES_PATH.getPath() + "\\" + yesterDayDate;
    }

    //获取今日图片路径
    public static String getToDayImagesPath() {
        LocalDate newDate = LocalDate.now();
        //得到上面图片下载保存的地址
        return SetUpMeaages.IMAGES_PATH.getPath() + "\\" + newDate;
    }

    //获取默认图片路径
    public static String getDefaultImagesPath() {
        return SetUpMeaages.IMAGES_PATH.getPath() + File.separator + "default";
    }

    //获取昨日是否下载了图片
    public static boolean yesterDayImagesItExist() {
        String s = getYesterDayImagesPath();
        return fileItExist(s);
    }

    //是否写入了默认壁纸
    public static boolean defaultImagesItExist() {
        String s = getDefaultImagesPath();
        return fileItExist(s);
    }


    //今天是否下载了图片
    public static boolean todayImagesItExist() {
        String s = getToDayImagesPath();
        File file = new File(s);
        if (file.exists()) {
            File[] files = file.listFiles();
            Integer downloadNum = Integer.valueOf(ConfigPropertiesUtil.get("downloadNum"));
            if (EmptyChecker.isEmpty(files)||files.length < downloadNum) {
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     *获取壁纸路径
     */
    public static String getWallpaperImagesPath() {

        String imagesPath = null;
        //今天是否下载了壁纸
        if (todayImagesItExist()) {
            imagesPath = getToDayImagesPath();
            //昨天是否下载了图片
        } else if (yesterDayImagesItExist()) {
            imagesPath = getYesterDayImagesPath();
        } else if (defaultImagesItExist()) {
            imagesPath = getDefaultImagesPath();
        } else {
            //将默认的壁纸写入存放壁纸的目录
            FirstRunImagesWrite firstRunImagesWrite = new FirstRunImagesWrite();
            firstRunImagesWrite.firstRunImagesWrite();
            imagesPath = getDefaultImagesPath();
        }
        return imagesPath;
    }

    /**
     *文件夹是否存在并且不是空文件夹
     */
    private static boolean fileItExist(String url) {
        File file = new File(url);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (EmptyChecker.isEmpty(files)) {
                return false;
            }
            return true;
        }
        return false;
    }
}
